package model;
import java.util.ArrayList;

/*****************************************
 * gameModel
 * 
 * Holds information about the game,
 * implements gameModelI
 * 
 * @author Stephen Gregory, Jun Wang
 *f
 */

public class gameModel implements gameModelI {
	
	private logManager logman;
	private frog frog;
	private log activeLog;
	private int numLetters;
	private int activeLogIndex;
	private int curScore;
	private int totalScore;
	private int stage;
	private int lives;
	private boolean gameOver;
	private leaderboard leader;
	
	public gameModel() {
		leader = new leaderboard();
	}
	
	@Override
	public void setupGame() {
		logman = new logManager("words.txt"); //initially easy word list?
		frog = new frog(0, 1000);
		activeLog = null;
		numLetters = 0;
		totalScore = 0;
		curScore = 0;
		stage = 0;
		lives = 2;
		gameOver = false;
	}

	@Override
	public ArrayList<log> getLogs() {
		return logman.getLogList();
	}

	@Override
	public frog getFrog() {
		return frog;
	}
	
	/**
	 * Method: getActiveLog
	 * @return the log with the word that is being typed
	 */

	@Override
	public log getActiveLog() {
		return activeLog;
	}

	@Override
	public void setActiveLog(log l) {
		activeLog = l;		
	}
	
	@Override
	public void activeLogTyped() {
		switch (activeLog.getType()) {
		case 1: incCurScore();// - double points by incrementing again
				break;
		case 2: logman.decVel(); //- slow down speed
				break;
		case 3: ++lives; // - extra life
				break;
		case 4: logman.incVel(); //- increase speed
				break;
		default: ;
				break;
		}
		activeLog.setType(0);
		setFrogPosVel(activeLog.getPosX()+60, activeLog.getPosY()-20, activeLog.getVelocityX());
		

		setNumLettersTyped(0);
		setActiveLog(null);				
	}

	@Override
	public void setFrogPosVel(int posX, int posY, int velX) {
		frog.setPos(posX, posY);
		frog.setVelocityX(velX);
		
	}
	
	/**
	 * Method: getNumLettersTyped
	 * @return the number of letters from the active log 
	 *         that have been typed
	 */

	@Override
	public int getNumLettersTyped() {
		return numLetters;
	}

	@Override
	public void setNumLettersTyped(int l) {
		numLetters = l;
		
	}

	@Override
	public int getActiveLogIndex() {
		return activeLogIndex;
	}

	@Override
	public void setActiveLogIndex(int i) {
		activeLogIndex = i;
	}

	@Override
	public void moveLogs(int time) {
		logman.moveLogs(time);
	}
	
	/**
	 * Method: getActiveLogPoint
	 * Purpose: to return the point value associated
	 *          with the active log when the entire word has 
	 *          been typed
	 */
	
	@Override
	public int getActiveLogPoint() {
		return activeLog.getPointValue();
	}
	
	/**
	 * Method: incScore
	 * Purpose: to increment the score of the player
	 *          currently the score is shown in the console
	 */
	
	@Override
	public void incCurScore() {
		curScore += activeLog.getPointValue();
	}
	
	/**
	 * Method: resetScore
	 * Purpose: reset the score to zero if the player
	 *          hits the edge of screen
	 */

	@Override
	public void resetCurScore() {
		curScore = totalScore;	
	}

	@Override
	public int getCurScore() {
		return curScore;
	}

	private void incTotalScore() {
		totalScore = curScore;
		resetCurScore();		
	}

	@Override
	public int getTotalScore() {
		// TODO Auto-generated method stub
		return totalScore;
	}

	@Override
	public void nextStage() {
		incTotalScore(); 
		stage++;
		frog.reset();

		if (stage < 1)
			logman = new logManager("words.txt", 1);
		else if (stage < 2)
			logman = new logManager("words.txt", 2);
		else if (stage < 3)
			logman = new logManager("words.txt", 3);
		else
			logman = new logManager("words.txt", 4);
}
	
	@Override
	public int getStage() {
		return stage;		
	}
	
	@Override
	public void lifeLost() {
		setActiveLog(null);
		setNumLettersTyped(0);
		getFrog().reset();
		resetCurScore();	
		lives--;
		if (lives == 0)
			gameOver = true;
		
	}

	@Override
	public int getLives() {
		return lives;
	}
	
	@Override
	public boolean isGameOver() {
		return gameOver;
	}

	@Override
	public ArrayList<record> getLeaderBoard() {
		// TODO Auto-generated method stub
		return leader.getRecords();
	}

	@Override
	public boolean isHighScore() {
		 if (leader.checkHighScore(totalScore) != -1)
			 return true;
		 else
			 return false;		
	}
	
	@Override
	public void setHighScore(String name) {
		 leader.addRecord(name, totalScore, leader.checkHighScore(totalScore));
	}
			
}
